﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ray2010
{
    public class InputFileAnalyzer
    {
        private String filepath;
        private Ray2010 ray2010;

        public InputFileAnalyzer(Ray2010 ray2010, string filepath)
        {
            this.ray2010 = ray2010;
            this.filepath = filepath;
            if (IsFileExistant())
            {
                if (IsFileValid())
                {
                    ExtractNodesFromFile();
                }
            }
        }


        public void ExtractNodesFromFile()
        {
            foreach (string line in File.ReadAllLines(filepath))
            {
                string[] temp = line.Split(' ');
                ray2010.nodes.Add(new Node(Int64.Parse(temp[0]), Int64.Parse(temp[1]),true, ray2010.sequence.GetNext()));
            }
        }

        public bool IsFileValid()
        {
            bool retValue = true;
            foreach (string line in File.ReadAllLines(filepath))
            {
                string[] temp = line.Split(' ');
                if (temp.Count() != 2)
                {
                    retValue = false;
                }
                foreach (string field in temp)
                {
                    long output;
                    if (!Int64.TryParse(field,out output))
                    {
                        retValue = false;
                    }
                }
            }
            return retValue;
        }

        public bool IsFileExistant()
        {
            return File.Exists(filepath);
        }
    }
}
